package com.oracle.lingshangejia.dialog;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.ArchivesTypeFormulateAdapter;
import com.oracle.lingshangejia.base.BaseDialogFragment;
import com.oracle.lingshangejia.bean.FormulateBean;
import com.oracle.lingshangejia.utils.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.tencent.bugly.beta.tinker.TinkerManager.getApplication;


/**
 * @松 on 2019/6/10 17:26
 * 居民档案个人信息签约类型 制定对话框
 */
public class ArchivesTypeFormulateDialog extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.rv)
    RecyclerView mRv;
    Unbinder unbinder;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    private Listener mListener;
    private ArchivesTypeFormulateAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.dialog_archives_type_formulate;
    }

    @Override
    protected void initWindow(Window window) {
        setLayoutParams(window, Gravity.CENTER,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
                0, 0);

    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        ButterKnife.bind(this, view);
        loadFormulate();
    }


    private void loadFormulate() {
        String uri = Constant.baseurl + "online/user/getDzbList";
        OkGo.getInstance().init(getApplication());
        JSONObject params = new JSONObject();
        try {
            String token = (String) SpUtils.get(getContext(), "token", "");
            int mark = (int) SpUtils.get(getContext(), "mark", 1);
            int id = (int) SpUtils.get(getContext(), "ysid", 1);
            params.put("userId", id);
            params.put("token", token);
            params.put("mark", mark);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        OkGo.<String>post(uri)
                .upJson(params.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String body = response.body().toString();
                        Gson gson = new Gson();
                        FormulateBean formulateBean = gson.fromJson(body,FormulateBean.class );
                        mRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        mAdapter = new ArchivesTypeFormulateAdapter(formulateBean.getData());
                        mAdapter.setOnItemClickListener(ArchivesTypeFormulateDialog.this);
                        mRv.setAdapter(mAdapter);



                    }

                    @Override
                    public void onError(Response<String> response) {
                        Toast.makeText(getContext(), "服务器暂时无法访问，稍后再试！", Toast.LENGTH_LONG).show();

                    }
                });
    }



    @OnClick({R.id.tv_cancel, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                returnSelectData();
                break;
        }
    }



    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        FormulateBean.DataBean dataBean = (FormulateBean.DataBean)adapter.getData().get(position);
        TextView tv = (TextView)view.findViewById(R.id.tv);
        TextView content = (TextView)view.findViewById(R.id.tv_content);
        if (dataBean.isSelect()){
            tv.setTextColor(getResources().getColor(R.color.black_111111));
            content.setTextColor(getResources().getColor(R.color.black_111111));

            dataBean.setSelect(false);
        }else {
            tv.setTextColor(getResources().getColor(R.color.colorBlue));
            content.setTextColor(getResources().getColor(R.color.colorBlue));
            dataBean.setSelect(true);
        }

    }

    private void returnSelectData(){
        List<FormulateBean.DataBean> dataBeanList = new ArrayList<>();
        for (FormulateBean.DataBean dataBean : mAdapter.getData()){
            if (dataBean.isSelect()){
                dataBeanList.add(dataBean);
            }

        }
        if (dataBeanList.size() >0){
            mListener.select(dataBeanList);
        }else {
            mListener.select(null);
        }
        dismiss();
    }

    public void setListener(Listener listener){
        mListener = listener;

    }

    public interface Listener{
        void select(List<FormulateBean.DataBean> list);
    }
}
